use axum::Json;
use reqwest::header;
use serde::{Deserialize, Serialize};
use serde_json::Value;

//天空
// static U: &str = "https://cj.lziapi.com/api.php/provide/vod/from/lzm3u8";
// 卧龙
static U: &str = "https://collect.wolongzyw.com/api.php/provide/vod";

pub async fn search_mov(Json(payload): Json<Value>) -> Json<Movlist> {
    let input = payload.get("search_term").unwrap().as_str().unwrap();
    println!("搜搜 {:#?} ", input);
    let a = httpget(U.to_string(), input.to_string()).await.unwrap();
    let list = a["list"].clone();
    let movlist: Movlist = serde_json::from_value(list).unwrap();
    Json(movlist)
}

/// 获取搜索列表
async fn httpget(url: String, wd: String) -> Result<Value, Box<dyn std::error::Error>> {
    // 构建请求头
    let mut headers = header::HeaderMap::new();
    headers.insert("accept", header::HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"));
    headers.insert(
        "accept-language",
        header::HeaderValue::from_static("zh-CN,zh;q=0.9,en;q=0.8"),
    );
    headers.insert(
        "cache-control",
        header::HeaderValue::from_static("max-age=0"),
    );
    headers.insert("priority", header::HeaderValue::from_static("u=0, i"));
    headers.insert(
        "sec-ch-ua",
        header::HeaderValue::from_static(
            "\"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Microsoft Edge\";v=\"140\"",
        ),
    );
    headers.insert("sec-ch-ua-mobile", header::HeaderValue::from_static("?0"));
    headers.insert(
        "sec-ch-ua-platform",
        header::HeaderValue::from_static("\"Linux\""),
    );
    headers.insert(
        "sec-fetch-dest",
        header::HeaderValue::from_static("document"),
    );
    headers.insert(
        "sec-fetch-mode",
        header::HeaderValue::from_static("navigate"),
    );
    headers.insert("sec-fetch-site", header::HeaderValue::from_static("none"));
    headers.insert("sec-fetch-user", header::HeaderValue::from_static("?1"));
    headers.insert(
        "upgrade-insecure-requests",
        header::HeaderValue::from_static("1"),
    );
    headers.insert("user-agent", header::HeaderValue::from_static("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0"));

    let c = reqwest::Client::new();
    let res = c
        .get(format!("{}?ac=detail&wd={}", url, wd))
        .timeout(std::time::Duration::from_secs(30))
        .headers(headers)
        .send()
        .await?;
    let body: Value = res.json().await?;
    // println!("Res:  {:#?}", body);
    Ok(body)
}

pub type Movlist = Vec<Mov>;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mov {
    pub vod_id: usize,
    /// 名称
    pub vod_name: String,
    pub vod_sub: String,
    pub vod_en: String,
    pub vod_tag: String,
    /// 图片
    pub vod_pic: String,
    /// 更新状态
    pub vod_remarks: String,
    /// 地区
    pub vod_area: String,
    /// 发布年
    pub vod_year: String,
    /// 更新时间
    pub vod_time: String,
    /// xxx
    pub vod_play_url: String,
    /// 影视类型
    pub type_name: String,
}

impl Mov {
    pub fn new() -> Self {
        Self {
            vod_id: 0,
            vod_name: "0".to_string(),
            vod_sub: "0".to_string(),
            vod_en: "0".to_string(),
            vod_tag: "0".to_string(),
            vod_pic: "0".to_string(),
            vod_remarks: "0".to_string(),
            vod_area: "0".to_string(),
            vod_year: "0".to_string(),
            vod_time: "0".to_string(),
            vod_play_url: "0".to_string(),
            type_name: "0".to_string(),
        }
    }
}

impl Default for Mov {
    fn default() -> Self {
        Mov::new()
    }
}
